import request from '/@/utils/request';
import {
	WechatBindMobileRequest,
	WechatBindMobileResponse,
	WechatLoginRequest,
	WechatLoginResponse,
	ChangeMobileRequest,
	ChangeMobileResponse,
	LoginBySmsRequest,
	LoginBySmsResponse,
	SendLoginSmsResponse,
	LoginConfigResponse,
	ChangePasswordBySmsRequest,
	ChangePasswordBySmsResponse,
} from './type';

/**
 * （不建议写成 request.post(xxx)，因为这样 post 时，无法 params 与 data 同时传参）
 *
 * 登录api接口集合
 * @method signIn 用户登录
 * @method signOut 用户退出登录
 */

export default function useLoginApi() {
	return {
		signIn: (data: object) => {
			return request({
				url: '/api/_account/loginjwt',
				method: 'post',
				data,
			});
		},
		refreshToken: () => {
			return request({
				url: '/api/_account/RefreshToken',
				method: 'post',
			});
		},
		signInSSO: (remoteToken: any) => {
			return request({
				url: '/api/_account/loginjwt?_remotetoken=' + remoteToken,
				method: 'post',
				data: {
					Account: '',
					Password: '',
					RemoteToken: remoteToken,
				},
			});
		},
		signOut: (data: object) => {
			return request({
				url: '/user/signOut',
				method: 'post',
				data,
			});
		},
		changePassword: (data: object) => {
			return request({
				url: '/api/_account/ChangePassword',
				method: 'post',
				data,
			});
		},

		getUserInfo: () => {
			return request({
				url: '/api/_account/checkuserinfo',
				method: 'get',
			});
		},
		getTimeLine: (flowname: string, entitytype: string, entityid: string) => {
			return request({
				url: '/api/common/gettimeline',
				method: 'get',
				params: { flowname, entitytype, entityid },
			});
		},
		/**
		 * 发送登录验证码
		 * @param {string} Mobile - 手机号
		 * @returns {Promise<SendLoginSmsResponse>} 返回发送结果
		 */
		sendVerificationCode: (Mobile: string, Type: string, OldPhone: string,): Promise<SendLoginSmsResponse> => {
			return request({
				url: '/api/_account/sendloginsms',
				method: 'post',
				data: { Mobile, Type, OldPhone, },
			});
		},
		/**
		 * 通过短信验证码修改密码
		 * @param {ChangePasswordBySmsRequest} data - 修改密码参数
		 * @returns {Promise<ChangePasswordBySmsResponse>} 返回修改结果
		 */
		changePasswordBySms: (data: ChangePasswordBySmsRequest): Promise<ChangePasswordBySmsResponse> => {
			return request({
				url: '/api/_Account/ChangePasswordBySms',
				method: 'post',
				data,
			});
		},
		/**
		 * 手机号验证码登录
		 * @param {LoginBySmsRequest} data - 登录参数，包含手机号和验证码
		 * @returns {Promise<LoginBySmsResponse>} 返回登录结果，包含token信息
		 */
		signInByPhone: (data: LoginBySmsRequest): Promise<LoginBySmsResponse> => {
			return request({
				url: '/api/_account/loginbysms',
				method: 'post',
				data,
			});
		},
		/**
		 * 修改手机号
		 * @param {ChangeMobileRequest} data - 修改手机号参数，包含新手机号和验证码
		 * @returns {Promise<ChangeMobileResponse>} 返回修改结果
		 */
		updatePhone: (data: ChangeMobileRequest): Promise<ChangeMobileResponse> => {
			return request({
				url: '/api/_account/changemobile',
				method: 'post',
				data,
			});
		},
		// 微信登录相关接口
		getWechatUrl: () => {
			return request({
				url: '/api/_account/getwechatloginurl',
				method: 'get',
			});
		},
		/**
		 * 微信登录
		 * @param {WechatLoginRequest} data - 微信登录参数，包含授权码和状态码
		 * @returns {Promise<WechatLoginResponse>} 返回登录结果，包含token信息
		 */
		signInWechatLogin: (data: WechatLoginRequest): Promise<WechatLoginResponse> => {
			return request({
				url: '/api/_account/wechatlogin',
				method: 'post',
				data,
			});
		},
		/**
		 * 绑定手机号
		 * @param {WechatBindMobileRequest} params - 绑定手机号所需参数
		 * @returns {Promise<WechatBindMobileResponse>} 返回绑定结果，包含token信息
		 */
		bindPhone: (params: WechatBindMobileRequest): Promise<WechatBindMobileResponse> => {
			return request({
				url: '/api/_account/wechatbindbymobile',
				method: 'post',
				data: params,
			});
		},
		/**
		 * 获取登录配置
		 * @returns {Promise<LoginConfigResponse>} 返回登录配置信息
		 */
		getLoginConfig: (): Promise<LoginConfigResponse> => {
			return request({
				url: '/api/_Account/GetLoginConfig',
				method: 'get',
			});
		},
	};
}
